//
//  IMB_DownImageHelper.m
//  InvestChina
//
//  Created by 龚传赞 on 14-4-2.
//  Copyright (c) 2014年 iMobile. All rights reserved.
//

#import "IMB_DownImageHelper.h"
#import <SDWebImage/UIImageView+WebCache.h>

@implementation IMB_DownImageHelper

+ (void)downImageWithURL:(NSURL *)imgURL imageView:(IMB_DownImageView *)imageView placeHolder:(UIImage *)placeHolder
{
    UIImage *cachedImage = [[[SDWebImageManager sharedManager] imageCache] imageFromDiskCacheForKey:[imgURL absoluteString]]; // 将需要缓存的图片加载进来
    //[SDManager diskImageExistsForURL:imgURL]; //判断是否已缓存该图片
    imageView.imgURL = imgURL;
    if (cachedImage) {
        // 如果Cache命中，则直接利用缓存的图片进行有关操作
        // Use the cached image immediatly
        imageView.image = cachedImage;
    } else {
        //添加指示器
        /* UIActivityIndicatorView *indicator;
        static NSInteger indicatorTag = 20140402;
        
        if (![imageView viewWithTag:indicatorTag]) {
            BOOL isNightMode = [[RNThemeManager sharedManager].currentThemeName isEqualToString:@"hicontrast"]?YES:NO;
            indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:!isNightMode? UIActivityIndicatorViewStyleGray:UIActivityIndicatorViewStyleWhite];
            [indicator setHidesWhenStopped:YES];
            
            [indicator setCenter:CGPointMake(ViewWidth(imageView)/2,ViewHeight(imageView)/2)];
            indicator.tag = indicatorTag;
            [imageView addSubview:indicator];
        }else{
            indicator = (UIActivityIndicatorView *)[imageView viewWithTag:indicatorTag];
        }
        [indicator startAnimating]; */
        
        // 如果Cache没有命中，则去下载指定网络位置的图片
        // Start an async download
        
        __weak IMB_DownImageView *weakImageView = imageView;
        
        [imageView sd_setImageWithURL:imgURL placeholderImage:placeHolder options:SDWebImageRetryFailed progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            // do something with image
            if ([weakImageView.imgURL isEqual:imgURL]) {
                if (image) {
                    //[indicator stopAnimating];
                    
                    weakImageView.alpha = 0;
                    weakImageView.image = image;
                    //NSLog(@"第%ld下载完成!",indexPath.row+1);
                    
                    [UIView animateWithDuration:1.0 animations:^{
                        weakImageView.alpha = 1.0;
                    }];
                    
                }else{
                    //[indicator stopAnimating];
                    //[SVProgressHUD showErrorWithStatus:@"图片下载失败"];
                }
            }
        }];
        
//        [SDManager downloadWithURL:imgURL
//                           options:SDWebImageRetryFailed
//                          progress:nil
//                         completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
//
//                                 // do something with image
//                             if ([imageView.imgURL isEqual:imgURL]) {
//                                 if (image) {
//                                     [indicator stopAnimating];
//                                     
//                                     imageView.alpha = 0;
//                                     imageView.image = image;
//                                     //NSLog(@"第%ld下载完成!",indexPath.row+1);
//                                     
//                                     [UIView animateWithDuration:1.0 animations:^{
//                                         imageView.alpha = 1.0;
//                                     }];
//                                     
//                                 }else{
//                                     [indicator stopAnimating];
//                                     //[SVProgressHUD showErrorWithStatus:@"图片下载失败"];
//                                 }
//                             }
//                             
//                             
//                         }];
         }
    
}



@end
